<template>
	<view class="ApproveContent">
		<eui-nav-bar :title="cTitle" color="#000" rightIcon="" />
		<view class="main-view">
			<view class="main-view-bg">
				<scroll-view scroll-y :style="{width:scrollWidth,height:scrollHeight}"
					style="padding: 20px 15px;box-sizing: border-box;">
					<eui-steps :options="list1" :active="active" type="approve">
						<template v-slot:content="scope" style="margin-top: 16rpx;">
							<view style="background: #F9FAFC; border-radius: 10rpx;margin-top: 16rpx;"
								v-if="scope.content">
								<view style="padding: 20rpx; color: #666666;font-size: 13px;">{{ scope.content }}</view>
							</view>

						</template>
					</eui-steps>
				</scroll-view>
			</view>
		</view>
		<view class="padding-20 background-color-fff">
			<ApproveBottomBar :data="approveInfo" :isStatus="false" :isNoteArea="true" :tabIndex="tabIndex"
				@change="onRefresh">
			</ApproveBottomBar>
		</view>
	</view>
</template>

<script setup type="ts">
	import { onLoad,onReady } from '@dcloudio/uni-app';
	import { ref } from 'vue';
	import service from '@/common/service';
	import { useMainStore } from '@/store/modules/store';
	import { useLoginStore } from '@/store/modules/useLoginStore';
	import ApproveBottomBar from'@/components/ApproveBottomBar.vue';
	import dayjs from 'dayjs';
	const mainStore = useMainStore();
	const LoginStore = useLoginStore();
	
	const statusBarHeight = ref(uni.getSystemInfoSync().statusBarHeight + 'px');
	const scrollWidth = ref((uni.getSystemInfoSync().windowWidth - 20) + 'px');
	const scrollHeight = ref((uni.getSystemInfoSync().windowHeight - uni.getSystemInfoSync().statusBarHeight - uni.getSystemInfoSync().safeAreaInsets.bottom - 168) + 'px');
	const dataList = ref([]);
	const reqUser = ref({});//前插后插人员信息
	const approveInfo = ref({});
	const cTitle = ref('');
	
	const active = ref(4);
	const list1 = ref([]);
	const PathList = ref([]);
	const tabIndex = ref('');
	
	
	const GetPreApproveChartPath = () =>{
		service.enjoyShowLoading('正在加载...');
		let request = {
		    url: LoginStore.EnjoyMain,
		    data: {
		        UniqueKey: "管易通40审批",
		        ObjectData: approveInfo.value.AppId,//mainStore.loginInfo.sUser,
		        MethodName: "GetPreApproveChartPath",
		    },
		}
		service.request(request, (res) => {
		    if (res && res.data) {
		        if (!res.data.HasException) {
					const ObjectData = res.data.ObjectData;
					console.log("GetPreApproveChartPath: " + JSON.stringify(ObjectData));
					if(ObjectData.length>0){
						active.value = ObjectData.length - 1;
						let AppDetail = '';
						const imgUrl = LoginStore.EnjoyMain.replace('/Enjoy/Service', '');
						let hasNodeId = '';
						ObjectData.map((item, index) => {
							if(item.ReplyStatus=='不同意'){
								AppDetail=item.AppDetail;
							}else if(item.ReplyStatus=='同意'){
								AppDetail=item.AppDetail;
							}else{
								AppDetail=item.AppDetail;
							}
							if(item.AppNodeType=='审批'){
								// 申请人
								list1.value.push({
									...item,
									title: '申请人',
									status:'发起审批',
									time:dayjs(item.AppDateTime).format('YYYY-MM-DD HH:mm:ss'),
									userInfo:[{
									    userName:item.NodeName ? item.NodeName : '无审批人',
									    userImg:imgUrl + '/File/DownLoadImage/' + item.AppReqUserNo
									}],
									content:AppDetail
								})
							}else{
								hasNodeId = '';
								for (let i = 0; i < list1.value.length; i++) {
									if(list1.value[i].NodeId==item.NodeId){
										hasNodeId=i
									}
								}
								if(hasNodeId===''){
									list1.value.push({
										...item,
										title: item.NodeName,
										status:item.AppNodeType=='已审批'?ConvertStatus(item.ReplyStatus):item.AppNodeType,
										time:item.AppDateTime,
										userInfo:[{
										    userName:item.AppNodeType=='正在审批'||item.AppNodeType=='已审批'?(item.AppedUsers ? item.AppedUsers : '无审批人'):(item.AppUsers ? item.AppUsers : '无审批人'),
											userImg:item.AppNodeType=='正在审批'||item.AppNodeType=='已审批'?(imgUrl + '/File/DownLoadImage/' + item.AppedUsersNo):(imgUrl + '/File/DownLoadImage/' + item.AppUsersNo)
										}],
										content:AppDetail,
										definedIcon:item.AppNodeType=='已审批'&&item.ReplyStatus=='C'?'icon-guanbi2':'',
										definedColor:item.AppNodeType=='已审批'&&item.ReplyStatus=='C'?'red':'',
									})
								}else{
									list1.value[hasNodeId].userInfo.push({
										userName:item.AppNodeType=='正在审批'||item.AppNodeType=='已审批'?(item.AppedUsers ? item.AppedUsers : '无审批人'):(item.AppUsers ? item.AppUsers : '无审批人'),
										userImg:item.AppNodeType=='正在审批'||item.AppNodeType=='已审批'?(imgUrl + '/File/DownLoadImage/' + item.AppedUsersNo):(imgUrl + '/File/DownLoadImage/' + item.AppUsersNo)
									})
								}
							}
						})
						for (let i = 0; i < list1.value.length; i++) {
							if(list1.value[i].AppNodeType=='正在审批'){
								active.value = i;
							}
						}
					}
		        } else {
		            service.enjoyShowToast(res.data.Exception.Message)
		        }
		    }
			service.enjoyHideLoading();
		}, () => {
			service.enjoyHideLoading();
		    console.log('失败')
		});
	}
	const ConvertStatus = e =>{
		// A未回复 B同意 C不同意 YA已阅未回复 Y已阅 N未阅
		switch (e){
			case 'A':
				return '未回复'
				break;
			case 'B':
				return '同意'
				break;
			case 'C':
				return '不同意'
				break;
			case 'YA':
				return '已阅未回复'
				break;
			case 'Y':
				return '已阅'
				break;
			case 'N':
				return '未阅'
				break;
			default:
				return e
				break;
		}
	}
	const GetBillAllApprovesChartPath = () =>{
		service.enjoyShowLoading('正在加载...');
		let request = {
		    url: LoginStore.EnjoyMain,
		    data: {
		        UniqueKey: "管易通40审批",
		        ObjectData: {
					BillKey: approveInfo.value.BillKey,
					ProjectName: approveInfo.value.ProjectName,
					MdCode: approveInfo.value.MdCode,
					IncludeCurrentPrePath: true,
				},
		        MethodName: "GetBillAllApprovesChartPath",
		    },
		}
		service.request(request, (res) => {
		    if (res && res.data) {
		        if (!res.data.HasException) {
					const ObjectData = res.data.ObjectData;
					console.log("GetBillAllApprovesChartPath: " + JSON.stringify(ObjectData));
					let nub = -1;
					for (let i = 0; i < ObjectData.length; i++) {
						if (ObjectData[i].StepAppInFo) {
							PathList.value.push({
								...ObjectData[i],
								detailList: []
							})
							nub += 1;
						} else {
							PathList.value[nub].detailList.push(ObjectData[i])
						}
					}
		        } else {
		            service.enjoyShowToast(res.data.Exception.Message)
		        }
		    }
			service.enjoyHideLoading();
		}, () => {
			service.enjoyHideLoading();
		    console.log('失败')
		});
	}
	const onRefresh = () =>{
		setTimeout(() => {
			SetCurrentPages();
			uni.navigateBack({
				delta: 1
			});
		}, 600)
	}
	const SetCurrentPages = () =>{
		try {
			let pages3 = getCurrentPages();
			let prevPage3 = pages3[pages3.length - 2];
			prevPage3.$vm.$.exposed.onRefresh();
		} catch (e) {
		}
	}
	onLoad(e=>{
		approveInfo.value = JSON.parse(e.item);
		tabIndex.value = e.tabIndex;
		cTitle.value = approveInfo.value.BillRealType+'审批';
		GetPreApproveChartPath();
		GetBillAllApprovesChartPath();
	})
	defineExpose({
		onRefresh
	})
</script>

<style lang="scss" scoped>
	@import '@/main.scss';

	.ApproveContent {
		position: fixed;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: #fff;
		z-index: 99;
	}

	.header-content {
		height: 88rpx;
		padding: 0 6px;
		background-color: #fff;
		align-items: center;
	}

	.flex {
		display: flex;
	}

	.flex-1 {
		flex: 1;
	}

	.justify-content-space-between {
		justify-content: space-between !important;
	}

	.main-view {
		flex: 1;
		flex-direction: column;
		padding: 20rpx;
		background-color: #f2f2f2;
	}

	.main-view-bg {
		background-color: #fff;
		border-radius: 20rpx;
		overflow: hidden;
	}

	.padding-left-28 {
		padding-left: 28rpx;
	}

	.font-size-28 {
		font-size: 28rpx;
	}

	.flex-direction-column {
		flex-direction: column !important;
	}

	.store-checked {
		background-color: rgba(249, 250, 252, 1) !important;
	}

	.sroll-store-list {
		padding: 20rpx 28rpx 20rpx 0;
		border-bottom: solid 1px rgba(238, 238, 238, 1);
		line-height: 1;
	}

	.color-888 {
		color: rgba(136, 136, 136, 1);
	}

	.margin-top-20 {
		margin-top: 20rpx;
	}

	.padding-20 {
		padding: 20rpx 20rpx 0 20rpx;
	}

	.background-color-fff {
		background-color: #fff;
	}
</style>